//
// Created by Tiany on 2021/6/22.
//

#include "SeqQueue.h"
#include "stdio.h"
#include "stdlib.h"

//创建空队列
PSeqQueue createEmptyQueue_seq(int m) {
    PSeqQueue pSeqQueue = malloc(sizeof(struct SeqQueue));
    if (pSeqQueue != NULL) {
        pSeqQueue->q = malloc(sizeof(struct DataType) * m);
        if (pSeqQueue->q) {
            pSeqQueue->MAXNUM = m;
            pSeqQueue->r = 0;/*队头队尾均设为0，表示空栈*/
            pSeqQueue->f = 0;
            return pSeqQueue;
        } else free(pSeqQueue);
    }
    printf("空间分配失败");
    return NULL;
}

//判断队列是否为空
int isEmptyQueue_seq(PSeqQueue pSeqQueue) {
    return (pSeqQueue->r == pSeqQueue->f);
}

//入队
void enQueue_seq(PSeqQueue pSeqQueue, struct DataType x) {
    if ((pSeqQueue->r + 1) % pSeqQueue->MAXNUM == pSeqQueue->f)/*当队尾差一个就等于最大长度，则约定队列已满*/
        printf("队列已满");
    else {
        pSeqQueue->q[pSeqQueue->r] = x;
        pSeqQueue->r = (pSeqQueue->r + 1) % pSeqQueue->MAXNUM;/*循环队列，取模，使其能够循环*/
    }
}

//出队
void deQueue_seq(PSeqQueue pSeqQueue) {
    if (pSeqQueue->r == pSeqQueue->f)
        printf("空队列");
    else pSeqQueue->f = (pSeqQueue->f + 1) % pSeqQueue->MAXNUM;
}

//取队列的头元素
struct DataType frontQueue_seq(PSeqQueue pSeqQueue) {
    if (pSeqQueue->r == pSeqQueue->f)
        printf("队列为空");
    else return (pSeqQueue->q[pSeqQueue->f]);
}

//数据写入队列
void writeInQueue_seq(PSeqQueue pSeqQueue, struct DataType x[], int length) {
    if (length > pSeqQueue->MAXNUM)
        printf("数据太长，队列长度不够");
    else {
        for (int i = 0; i < length; i++) {
            enQueue_seq(pSeqQueue, x[i]);
        }
    }
}

//读出队列所以元素
void readAllQueue_seq(PSeqQueue pSeqQueue) {
    if (isEmptyQueue_seq(pSeqQueue))
        printf("空队列哦");
    else {
        for (int i = pSeqQueue->f; i < pSeqQueue->r; i++) {
            printf("%d  ", pSeqQueue->q[i]);
        }
    }

}
